<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('新增学生')"/>
    <th:block th:include="include :: ztree-css"/>
</head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form class="form-horizontal m" id="form-role-add">
        <div class="form-group">
            <label class="col-sm-3 control-label ">学生姓名：</label>
            <div class="col-sm-8">
                <input class="form-control" id="studentName" name="studentName" required type="text">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label"><span style="color: red; ">*</span>所在系号：</label>
            <div class="col-sm-8">
                <div class="input-group">
                    <input id="treeId" name="treeId" type="hidden">
                    <input class="form-control" id="treeName" name="treeName" onclick="selectDeptTree()" placeholder="请选择所在系"
                           required type="text">
                    <span class="input-group-addon"><i class="fa fa-search"></i></span>
                </div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label"><span style="color: red; ">*</span>登录账号：</label>
            <div class="col-sm-8">
                <input class="form-control" id="loginName" maxlength="30" name="loginName" placeholder="请输入登录账号"
                       required type="text">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label"><span style="color: red; ">*</span>登录密码：</label>
            <div class="col-sm-8">
                <input class="form-control" name="password" placeholder="请输入登录密码" required
                       th:value="${@config.getKey('sys.user.initPassword')}" type="password">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">入学成绩：</label>
            <div class="col-sm-8">
                <input class="form-control" id="studentGrade" name="studentGrade" required type="text">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">出生日期：</label>
            <div class="col-sm-8">
                <input class="form-control" id="studentBirthday" name="studentBirthday" placeholder="年-月-日" required
                       type="text">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">学生性别：</label>
            <div class="col-sm-8">
                <select class="form-control" id="studentSex" name="studentSex" required>
                    <option value="0">男</option>
                    <option value="1">女</option>
                    <option value="2">未知</option>
                </select>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">用户角色：</label>
            <div class="col-xs-8">
                <label class="check-box" th:each="role:${roles}">
                    <input name="role" th:disabled="${role.status == '1'}" th:text="${role.roleName}" th:value="${role.roleId}"
                           type="checkbox">
                </label>
            </div>
        </div>
    </form>
</div>
<th:block th:include="include :: footer"/>
<th:block th:include="include :: ztree-js"/>
<th:block th:include="include :: datetimepicker-js"/>
<script type="text/javascript">
    var prefix = ctx + "system/user";
    $(function () {
        $("#studentBirthday").datetimepicker({
            format: "yyyy-mm-dd",
            minView: "month",
            autoclose: true
        });
    });

    $("#form-user-add").validate({
        onkeyup: false,
        rules: {
            loginName: {
                minlength: 2,
                maxlength: 20,
                remote: {
                    url: prefix + "/checkLoginNameUnique",
                    type: "post",
                    dataType: "json",
                    data: {
                        "loginName": function () {
                            return $.common.trim($("#loginName").val());
                        }
                    },
                    dataFilter: function (data, type) {
                        return $.validate.unique(data);
                    }
                }
            },
            password: {
                minlength: 5,
                maxlength: 20
            },
        },
        messages: {
            "loginName": {
                remote: "用户已经存在"
            }
        },
        focusCleanup: true
    });

    function submitHandler() {
        if ($.validate.form()) {
            add();
        }
    }

    function add() {
        if ($.validate.form()) {
            var studentName = $("input[name='studentName']").val();
            var treeId = $("input[name='treeId']").val();
            var loginName = $("input[name='loginName']").val();
            var password = $("input[name='password']").val();
            var studentGrade = $("input[name='studentGrade']").val();
            var studentBirthday = $("input[name='studentBirthday']").val();
            var studentSex = $("#studentSex").val();
            var roleIds = $.form.selectCheckeds("role");
            $.ajax({
                cache: true,
                type: "POST",
                url: ctx + "project/student/addStudent",
                data: {
                    "studentName": studentName,
                    "treeId": treeId,
                    "loginName": loginName,
                    "password": password,
                    "studentGrade": studentGrade,
                    "studentBirthday": studentBirthday,
                    "studentSex": studentSex,
                    "roleIds": roleIds
                },
                async: false,
                error: function (request) {
                    $.modal.alertError("系统错误");
                },
                success: function (data) {
                    $.operate.successCallback(data);
                }
            });
        }
    }

    /*用户管理-新增-选择部门树*/
    function selectDeptTree() {
        var treeId = $("#treeId").val();
        var deptId = $.common.isEmpty(treeId) ? "100" : $("#treeId").val();
        var url = ctx + "system/dept/selectDeptTree/" + deptId;
        var options = {
            title: '选择院系',
            width: "380",
            url: url,
            callBack: doSubmit
        };
        $.modal.openOptions(options);
    }

    function doSubmit(index, layero) {
        var tree = layero.find("iframe")[0].contentWindow.$._tree;
        if ($.tree.notAllowParents(tree)) {
            var body = layer.getChildFrame('body', index);
            $("#treeId").val(body.find('#treeId').val());
            $("#treeName").val(body.find('#treeName').val());
            layer.close(index);
        }
    }
</script>
</body>
</html>
